package br.com.livro.javapersistence.heranca.model;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.OneToMany;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.hibernate.annotations.Cascade;

import br.com.livro.javapersistence.commons.model.CommonField;

@Entity
@Inheritance(strategy = InheritanceType.JOINED)
public abstract class Pessoa {

	@Id
	@GeneratedValue
	@Column
	private Long pessoaId;
	
	@Column
	private Long userId;

	@Embedded
	private CommonField commonField;
	
	@OneToMany(fetch = FetchType.LAZY, mappedBy = "solicitante", cascade = {
			CascadeType.MERGE,
			CascadeType.PERSIST,
			CascadeType.REMOVE
	})
	@Cascade(value = org.hibernate.annotations.CascadeType.DELETE_ORPHAN)
	//@Where(clause = "ativo = 1")
	private List<Solicitacao> solicitacoes = new ArrayList<Solicitacao>();

	public Pessoa() {
	}

	/**
	 * @param userId
	 * @param commonField
	 */
	public Pessoa(Long userId, CommonField commonField) {
		this.setCommonField(commonField);
		this.setUserId(userId);
	}

	/**
	 * @param solicitacao
	 */
	public void addSolicitacao(Solicitacao solicitacao){
		solicitacao.setSolicitante(this);
		this.getSolicitacoes().add(solicitacao);
	}
	
	public String toString(){
		return ToStringBuilder.reflectionToString(this);
	}
	
	public Long getUserId() {
		return userId;
	}

	public void setUserId(Long userId) {
		this.userId = userId;
	}

	public CommonField getCommonField() {
		return commonField;
	}

	public void setCommonField(CommonField commonField) {
		this.commonField = commonField;
	}

	public List<Solicitacao> getSolicitacoes() {
		return solicitacoes;
	}

	public void setSolicitacoes(List<Solicitacao> solicitacoes) {
		this.solicitacoes = solicitacoes;
	}

	public Long getPessoaId() {
		return pessoaId;
	}

	public void setPessoaId(Long pessoaId) {
		this.pessoaId = pessoaId;
	}
}
